//Thu Sep 15 21:42:11 CDT 2011
import java.util.Arrays;

public class MatchNumbersEasy {
	public String[] dp;

	public String maxNumber(int[] matches, int n) {
		int N = matches.length;
		dp = new String[55];
		for (int i = 1; i <= n; i++)
			dp[i] = "";
		for (int i = 0; i < N; i++) {
			dp[matches[i]] = Integer.toString(i);
		}

		for (int i = 2; i <= n; i++) {
			for (int j = 1; j <= i / 2; j++) {
				String tmp = dp[j] + dp[i - j];
				tmp = sortReversely(tmp);
				if (tmp.length() > dp[i].length() && tmp.charAt(0) != '0')
					dp[i] = tmp;
				else if (tmp.length() == dp[i].length()
						&& tmp.compareTo(dp[i]) > 0)
					dp[i] = tmp;
			}
		}
		if (dp[n].length() == 0 || dp[n].charAt(0) == '0')
			return "0";
		return dp[n];
	}

	public static String sortReversely(String s) {
		char[] temp = s.toCharArray();
		Arrays.sort(temp);
		String ret = new String(temp);
		ret = new StringBuffer(ret).reverse().toString();
		return ret;
	}

	 
}
// Powered by [KawigiEdit] 2.0!
